package com.nononsenseapps.feeder.util;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;


import timber.log.Timber;

public class IntentUtils {

    public static  void startActivityByUrl(Context mContext,String link,String title, String mineTypeSmaple){
        Timber.d("startActivityByUrl-link:"+link+"\tmineType:"+mineTypeSmaple);
        Intent intent =new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        String mineType=null;

        MediaFileUtil.MediaFileType fileType=MediaFileUtil.getFileType(link);
        if(fileType!=null)
            mineType=fileType.mimeType;

        if(mineType==null) {
            fileType=MediaFileUtil.getFileType(MediaFileUtil.removeParams(link));
            if(fileType!=null)
                mineType=fileType.mimeType;
        }

        if(mineType==null&&mineTypeSmaple!=null){
            mineType=mineTypeSmaple;
        }

        Uri uri  = Uri.parse(link);
        if(mineType!=null){
            intent.putExtra(Intent.EXTRA_SUBJECT, title);
            intent.setDataAndType(uri,mineType);
        }else{
            intent.setData(uri);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Timber.d("startActivityByUrl-link: ${intent}\t last mineType:$mineType");
        try {
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            /*Toast.makeText(mContext, R.string.no_activity_for_link, Toast.LENGTH_SHORT).show();*/
            Toast.makeText(mContext, "no_activity_for_link", Toast.LENGTH_SHORT).show();
        }
    }
}